package com.opd.soeghtool;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

import java.util.Calendar;

public class MyDrawView extends View implements Runnable{

    private final String TAG="SOEDRAW";
    private Paint paint = null,pSingleDown=null,pMultiDown=null,pSingleUp=null,pMultiUp=null;
    private Paint paint1 = null,paintTH = null, paintTV = null,paintS = null,paintS1=null;
    private int x = 0;
    private int y = 0;

    int mImageHalfSize = 25;
    //椭圆形的left，right,top,buttom初始位置
    private float lx = 0.0f;
    private float rx = 50.0f;
    private float ty = 0.0f;
    private float by = 100.0f;

    Context mContex;
    private int screenWidth= 1080;//Resources.getSystem().getDisplayMetrics().widthPixels;
    private int screenHeight = 1920;//Resources.getSystem().getDisplayMetrics().heightPixels;
    boolean bIsDrawCross = false,bHasNotchScreen=false,bIsAdvSettingGuide=false,bIsEnterGameGuide=false,bIsFloatMenuGuide=false;

    int screenWidth_Pass=1920,screenHeight_Pass=1080;
    /**
     * 椭圆形的中心点坐标（要始终保证中心点的这两个值不变）
     */
    private Handler handler = new Handler();
    private float cx = 25.0f;
    private float cy = 50.0f;

    int mStep = 0;

    boolean g_bIsVirtualMacroMode =false;
    byte g_u8VirtualMacroLen =0;
    int g_u32VirtualMacroData[];


    public MyDrawView(Context context) {
        super(context);
        mContex = context;

        this.setFocusable(true);//允许获取上层焦点
        paint = new Paint();
        paint.setColor(0xffffff00);

        paint1 = new Paint();
        paint1.setColor(0x20ff0000);

        paintS = new Paint(Paint.ANTI_ALIAS_FLAG);
        paintS.setStyle(Paint.Style.STROKE);
        paintS.setStrokeWidth(12);
        paintS.setColor(0xffFF8028);    //FFA048
        paintS1 = new Paint(paintS);
        paintS1.setColor(0xff00FFFF);

        pSingleDown = new Paint(Paint.ANTI_ALIAS_FLAG);
        pSingleDown.setStyle(Paint.Style.STROKE);
        pSingleDown.setStrokeWidth(10);
        pSingleDown.setColor(0xffFF0000);    //FFA048
        pSingleUp = new Paint(pSingleDown);
        pSingleUp.setColor(0xff00FF00);
        pMultiDown = new Paint(pSingleDown);
        pMultiDown.setColor(0xffFF00FF);
        pMultiUp = new Paint(pSingleDown);
        pMultiUp.setColor(0xff00FFFF);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(3);
        paint.setTextSize(32);

        paintTH = new Paint(paint);
        paintTV = new Paint(paint);
        paintTH.setColor(Color.RED);
        paintTV.setColor(Color.GREEN);

        mImageHalfSize = dip2px(mContex,20);
        handler.postDelayed(this, 1000);

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        display.getRealMetrics(dm);
//        new GameViewThread().start();
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
        /*
        if(Transfer_Con.ENABLE_DIFFERENT_RESOLUTION_GB) {
            if ((screenHeight_Pass != screenHeight) && ((screenHeight == 1128) || (screenHeight == 752))) {
                screenHeight = screenHeight_Pass;
            }

            if ((screenWidth_Pass != screenWidth) && ((screenWidth == 2400) || (screenWidth == 1600))) {
                screenWidth = screenWidth_Pass;
            }
        }

         */
        g_u32VirtualMacroData = new int[120];
        Log.d(TAG,String.format("wid: %d, hei: %d,",screenWidth,screenHeight));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //canvas.drawLine(x, 0, x, 480, paint);
        //Rect rec = new Rect((int)lx,(int)ty,500,500);
        //RectF rectf = new RectF(lx, ty, rx, by);
        //canvas.drawOval(rectf, paint);
        //canvas.drawRect(rec,paint);
        Calendar calendar = Calendar.getInstance();
        int currentSecond = calendar.get(Calendar.SECOND);
        if (bIsDrawCross) {
            canvas.drawLine(x, 0, x, screenHeight, paintTH);
            canvas.drawLine(0, y, screenWidth, y, paintTV);
        }
        if(bIsAdvSettingGuide)
        {
        if(mStep==1) {
            canvas.drawCircle(dip2px(mContex, 300), dip2px(mContex, 300), dip2px(mContex, 40), paintS);

            canvas.drawCircle(dip2px(mContex, 120), dip2px(mContex, 120), dip2px(mContex, 40), paintS1);

            if ((currentSecond % 2) == 0)
                drawArrow(canvas, dip2px(mContex, 120), dip2px(mContex, 120), dip2px(mContex, 300), dip2px(mContex, 300), 30, 50, paintTH);
            else
                drawArrow(canvas, dip2px(mContex, 120), dip2px(mContex, 120), dip2px(mContex, 220), dip2px(mContex, 220), 30, 50, paintTH);
        }
        else if(mStep ==2)
        {
            canvas.drawCircle(dip2px(mContex, 300), dip2px(mContex, 300), dip2px(mContex, 40), paintS);
            if ((currentSecond % 2) == 0)
                canvas.drawRoundRect(new RectF((screenWidth/4)+dip2px(mContex,115), 0, screenWidth/4+dip2px(mContex,155), dip2px(mContex,40)), 20, 20, paintS);
            else
                canvas.drawRoundRect(new RectF((screenWidth/4)+dip2px(mContex,115), 0, screenWidth/4+dip2px(mContex,155), dip2px(mContex,40)), 20, 20, paintS1);
        }
        else if(mStep ==3)
        {
            if ((currentSecond % 2) == 0)
                canvas.drawRoundRect(new RectF((screenWidth/4)+dip2px(mContex,75), 0, screenWidth/4+dip2px(mContex,115), dip2px(mContex,40)), 20, 20, paintS);
            else
                canvas.drawRoundRect(new RectF((screenWidth/4)+dip2px(mContex,75), 0, screenWidth/4+dip2px(mContex,115), dip2px(mContex,40)), 20, 20, paintS1);
        }
        else if(mStep ==4)
        {
            if ((currentSecond % 2) == 0)
                canvas.drawRoundRect(new RectF((screenWidth/4)+dip2px(mContex,35), 0, screenWidth/4+dip2px(mContex,75), dip2px(mContex,40)), 20, 20, paintS);
            else
                canvas.drawRoundRect(new RectF((screenWidth/4)+dip2px(mContex,35), 0, screenWidth/4+dip2px(mContex,75), dip2px(mContex,40)), 20, 20, paintS1);

        }
        else if(mStep ==0)
        {
            if ((currentSecond % 2) == 0)
                canvas.drawRoundRect(new RectF((screenWidth/4)-dip2px(mContex,5), 0, screenWidth/4+dip2px(mContex,35), dip2px(mContex,40)), 20, 20, paintS);
            else
                canvas.drawRoundRect(new RectF((screenWidth/4)-dip2px(mContex,5), 0, screenWidth/4+dip2px(mContex,35), dip2px(mContex,40)), 20, 20, paintS1);
        }
        else if(mStep==5) {


            if ((currentSecond % 2) == 0)
                canvas.drawCircle(dip2px(mContex, 300), dip2px(mContex, 300), dip2px(mContex, 40), paintS);
            else
                canvas.drawCircle(dip2px(mContex, 300), dip2px(mContex, 300), dip2px(mContex, 40), paintS1);
        }
        else if(mStep==6) {
            int mNotchSize = 0;
            if (bHasNotchScreen) {
                mNotchSize = 100;
            }
            if ((currentSecond % 2) == 0)
                canvas.drawRoundRect(new RectF(mNotchSize, dip2px(mContex, 0), mNotchSize + dip2px(mContex, 140), screenHeight - 12), 20, 20, paintS);
            else
                canvas.drawRoundRect(new RectF(mNotchSize, dip2px(mContex, 0), mNotchSize + dip2px(mContex, 140), screenHeight - 12), 20, 20, paintS1);
        }
        }
        else if(bIsEnterGameGuide)
        {
            final int  width = screenWidth;
            if(mStep==0) {
                if ((currentSecond % 2) == 0)
                    canvas.drawRoundRect(new RectF((screenWidth / 5 + dip2px(mContex, 60)), dip2px(mContex, 20), screenWidth / 5 + dip2px(mContex, 180), dip2px(mContex, 150)), 20, 20, paintS);
                else
                    canvas.drawRoundRect(new RectF(screenWidth / 5 + dip2px(mContex, 60), dip2px(mContex, 20), screenWidth / 5 + dip2px(mContex, 180), dip2px(mContex, 150)), 20, 20, paintS1);
            }
            else if(mStep==1)
            {
                if ((currentSecond % 2) == 0)
                    canvas.drawRoundRect(new RectF(screenWidth / 5 + dip2px(mContex, 60), dip2px(mContex, 20), screenWidth / 5 + dip2px(mContex, 88), dip2px(mContex, 150)), 20, 20, paintS);
                else
                    canvas.drawRoundRect(new RectF(screenWidth / 5 + dip2px(mContex, 60), dip2px(mContex, 20), screenWidth / 5 + dip2px(mContex, 88), dip2px(mContex, 150)), 20, 20, paintS1);
            }
            else if(mStep==2)
            {
                if ((currentSecond % 2) == 0)
                    canvas.drawRoundRect(new RectF(screenWidth / 5 + dip2px(mContex, 88), dip2px(mContex, 20), screenWidth / 5 + dip2px(mContex, 147), dip2px(mContex, 150)), 20, 20, paintS);
                else
                    canvas.drawRoundRect(new RectF(screenWidth / 5 + dip2px(mContex, 88), dip2px(mContex, 20), screenWidth / 5 + dip2px(mContex, 147), dip2px(mContex, 150)), 20, 20, paintS1);
            }
            else if(mStep==3)
            {
                if ((currentSecond % 2) == 0)
                    canvas.drawRoundRect(new RectF(screenWidth / 5 + dip2px(mContex, 147), dip2px(mContex, 20), screenWidth / 5 + dip2px(mContex, 180), dip2px(mContex, 150)), 20, 20, paintS);
                else
                    canvas.drawRoundRect(new RectF(screenWidth / 5 + dip2px(mContex, 147), dip2px(mContex, 20), screenWidth / 5 + dip2px(mContex, 180), dip2px(mContex, 150)), 20, 20, paintS1);
            }
            else if(mStep==4)
            {
                if ((currentSecond % 2) == 0)
                    canvas.drawCircle(screenWidth / 5+dip2px(mContex, 15), dip2px(mContex, 15), dip2px(mContex, 15), paintS);
                else
                    canvas.drawCircle(screenWidth / 5+dip2px(mContex, 15), dip2px(mContex, 15), dip2px(mContex, 15), paintS1);
            }
            else if(mStep==5)
            {
                if ((currentSecond % 2) == 0)
                    canvas.drawCircle(screenWidth / 5+dip2px(mContex, 45), dip2px(mContex, 15), dip2px(mContex, 15), paintS);
                else
                    canvas.drawCircle(screenWidth / 5+dip2px(mContex, 45), dip2px(mContex, 15), dip2px(mContex, 15), paintS1);
            }
        }
        else if(bIsFloatMenuGuide)
        {
            final int  width = screenWidth;
            if(mStep==0) {
                if ((currentSecond % 2) == 0)
                    canvas.drawRoundRect(new RectF((screenWidth /2 - dip2px(mContex, 150)), dip2px(mContex, 10), screenWidth / 2 - dip2px(mContex, 95), dip2px(mContex, 50)), 20, 20, paintS);
                else
                    canvas.drawRoundRect(new RectF((screenWidth/2 - dip2px(mContex, 150)), dip2px(mContex, 10), screenWidth / 2 - dip2px(mContex, 95), dip2px(mContex, 50)), 20, 20, paintS1);
            }
            else if(mStep==1)
            {
                if ((currentSecond % 2) == 0)
                    canvas.drawRoundRect(new RectF((screenWidth /2 - dip2px(mContex, 90)), dip2px(mContex, 10), screenWidth / 2 - dip2px(mContex, 35), dip2px(mContex, 50)), 20, 20, paintS);
                else
                    canvas.drawRoundRect(new RectF((screenWidth/2 - dip2px(mContex, 90)), dip2px(mContex, 10), screenWidth / 2 - dip2px(mContex, 35), dip2px(mContex, 50)), 20, 20, paintS1);
            }
            else if(mStep==2)
            {
                if ((currentSecond % 2) == 0)
                    canvas.drawRoundRect(new RectF((screenWidth /2 - dip2px(mContex, 28)), dip2px(mContex, 10), screenWidth / 2 + dip2px(mContex, 30), dip2px(mContex, 50)), 20, 20, paintS);
                else
                    canvas.drawRoundRect(new RectF((screenWidth/2 - dip2px(mContex, 28)), dip2px(mContex, 10), screenWidth / 2 + dip2px(mContex, 30), dip2px(mContex, 50)), 20, 20, paintS1);
           }
            else if(mStep==3)
            {
                if ((currentSecond % 2) == 0)
                    canvas.drawRoundRect(new RectF((screenWidth /2 + dip2px(mContex, 35)), dip2px(mContex, 10), screenWidth / 2 + dip2px(mContex, 90), dip2px(mContex, 50)), 20, 20, paintS);
                else
                    canvas.drawRoundRect(new RectF((screenWidth/2 + dip2px(mContex, 35)), dip2px(mContex, 10), screenWidth / 2 + dip2px(mContex, 90), dip2px(mContex, 50)), 20, 20, paintS1);
            }
            else if(mStep==4)
            {
                if ((currentSecond % 2) == 0)
                    canvas.drawRoundRect(new RectF((screenWidth /2 + dip2px(mContex, 95)), dip2px(mContex, 10), screenWidth / 2 + dip2px(mContex, 150), dip2px(mContex, 50)), 20, 20, paintS);
                else
                    canvas.drawRoundRect(new RectF((screenWidth/2 + dip2px(mContex, 95)), dip2px(mContex, 10), screenWidth / 2 + dip2px(mContex, 150), dip2px(mContex, 50)), 20, 20, paintS1);
            }
            else if(mStep==5)
            {
                if ((currentSecond % 2) == 0)
                    canvas.drawCircle(screenWidth/4+dip2px(mContex, 15), dip2px(mContex, 15), dip2px(mContex, 15), paintS);
                else
                    canvas.drawCircle(screenWidth/4+dip2px(mContex, 15), dip2px(mContex, 15), dip2px(mContex, 15), paintS1);
            }
            else if(mStep==6) {
                if ((currentSecond % 2) == 0)
                    canvas.drawRoundRect(new RectF((screenWidth/2 - dip2px(mContex, 60)), dip2px(mContex, 50), screenWidth /2 - dip2px(mContex, 30), dip2px(mContex, 180)), 20, 20, paintS);
                else
                    canvas.drawRoundRect(new RectF((screenWidth/2 - dip2px(mContex, 60)), dip2px(mContex, 50), screenWidth /2 - dip2px(mContex, 30), dip2px(mContex, 180)), 20, 20, paintS1);
            }
            else if(mStep==7)
            {
                if ((currentSecond % 2) == 0)
                    canvas.drawRoundRect(new RectF((screenWidth/2 - dip2px(mContex, 30)), dip2px(mContex, 50), screenWidth /2 + dip2px(mContex, 30), dip2px(mContex, 180)), 20, 20, paintS);
                else
                    canvas.drawRoundRect(new RectF((screenWidth/2 - dip2px(mContex, 30)), dip2px(mContex, 50), screenWidth /2 + dip2px(mContex, 30), dip2px(mContex, 180)), 20, 20, paintS1);
            }
            else if(mStep==8)
            {
                if ((currentSecond % 2) == 0)
                    canvas.drawRoundRect(new RectF((screenWidth/2 + dip2px(mContex, 30)), dip2px(mContex, 50), screenWidth /2 + dip2px(mContex, 60), dip2px(mContex, 180)), 20, 20, paintS);
                else
                    canvas.drawRoundRect(new RectF((screenWidth/2 + dip2px(mContex, 30)), dip2px(mContex, 50), screenWidth /2 + dip2px(mContex, 60), dip2px(mContex, 180)), 20, 20, paintS1);
            }
        }
        else if(g_bIsVirtualMacroMode)
        {
            for(int a=0;a<g_u8VirtualMacroLen;)
            {
                int pointCounter=((g_u32VirtualMacroData[a+3]&0xff0000)>>16);
                byte text_x =3;
                if(a>=60)
                {
                    text_x = 6;
                }
                if((g_u32VirtualMacroData[a]&0xff0000)==0x10000)    // Multi
                {
                    for(int k=0;k<pointCounter;k++)
                    {
                        if(a>=60)
                        {
                            text_x = 6;
                        }
                        canvas.drawCircle(g_u32VirtualMacroData[a + 1], g_u32VirtualMacroData[a + 2], dip2px(mContex, 8), pMultiDown);
                        canvas.drawCircle(g_u32VirtualMacroData[a + 4], g_u32VirtualMacroData[a + 5], dip2px(mContex, 10), pMultiUp);
                        if ((g_u32VirtualMacroData[a + 1] == g_u32VirtualMacroData[a + 4]) && (g_u32VirtualMacroData[a + 2] == g_u32VirtualMacroData[a + 5])) {
                            canvas.drawText(String.valueOf(a/6), g_u32VirtualMacroData[a + 1] - dip2px(mContex, text_x), g_u32VirtualMacroData[a + 2] + dip2px(mContex, 3), paintTH);
                        } else {
                            canvas.drawText(String.valueOf(a/6), g_u32VirtualMacroData[a + 1] - dip2px(mContex, text_x), g_u32VirtualMacroData[a + 2] + dip2px(mContex, 3), paintTH);
                            canvas.drawText(String.valueOf(a/6), g_u32VirtualMacroData[a + 4] - dip2px(mContex, text_x), g_u32VirtualMacroData[a + 5] + dip2px(mContex, 3), paintTV);
                            drawArrow(canvas, g_u32VirtualMacroData[a + 1], g_u32VirtualMacroData[a + 2], g_u32VirtualMacroData[a + 4], g_u32VirtualMacroData[a + 5], 30, 50, paintTH);
                        }
                        a+=6;
                    }
                }
                else
                {
                    canvas.drawCircle(g_u32VirtualMacroData[a + 1], g_u32VirtualMacroData[a + 2], dip2px(mContex, 8), pSingleDown);
                    canvas.drawCircle(g_u32VirtualMacroData[a + 4], g_u32VirtualMacroData[a + 5], dip2px(mContex, 10), pSingleUp);
                    if ((g_u32VirtualMacroData[a + 1] == g_u32VirtualMacroData[a + 4]) && (g_u32VirtualMacroData[a + 2] == g_u32VirtualMacroData[a + 5])) {
                        canvas.drawText(String.valueOf(a / 6), g_u32VirtualMacroData[a + 1] - dip2px(mContex, text_x), g_u32VirtualMacroData[a + 2] + dip2px(mContex, 3), paintTH);
                    } else {
                        canvas.drawText(String.valueOf(a / 6), g_u32VirtualMacroData[a + 1] - dip2px(mContex, text_x), g_u32VirtualMacroData[a + 2] + dip2px(mContex, 3), paintTH);
                        canvas.drawText(String.valueOf(a / 6), g_u32VirtualMacroData[a + 4] - dip2px(mContex, text_x), g_u32VirtualMacroData[a + 5] + dip2px(mContex, 3), paintTV);
                        drawArrow(canvas, g_u32VirtualMacroData[a + 1], g_u32VirtualMacroData[a + 2], g_u32VirtualMacroData[a + 4], g_u32VirtualMacroData[a + 5], 30, 50, paintTH);
                    }
                    a+=6;
                }
            }
        }
        //canvas.drawCircle(500,500,30,paint);
        //if((Math.abs(500-x)<=20)&&
        //Rect rec1 = new Rect((int)lx+100,(int)ty+100,400,400);
        //canvas.drawRect(rec1,paint1);
        //canvas.drawRoundRect(new RectF(0, 0, 1000, 1000), 300, 300, paintS);
        //Bitmap bitmap=null;
        //bitmap=((BitmapDrawable)getResources().getDrawable(R.drawable.btn_a)).getBitmap();
        //canvas.drawBitmap(bitmap,x,y,paintS);
    }

    /*class GameViewThread extends Thread{
        @Override
        public void run() {
            while(x <= 480){
                x+=5;
                postInvalidate();
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }*/
    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //Log.i(TAG,"ACTION_DOWN "+String.valueOf(event.getRawX())+" : "+String.valueOf(event.getRawY()));
                x = (int)event.getRawX();
                y = (int)event.getRawY();
                bIsDrawCross = true;
                break;
            case MotionEvent.ACTION_MOVE:
                //Log.i(TAG,"ACTION_MOVE");
                x = (int)event.getRawX();
                y = (int)event.getRawY();
                bIsDrawCross = true;
                break;
            case MotionEvent.ACTION_UP:
                //Log.i(TAG,"ACTION_UP");
                bIsDrawCross = false;
                break;
        }


        /*
        //Log.d("SOEUGS",String.format("x=%d,y=%d",(int)event.getRawX(),(int)event.getRawY()));
        //触摸位置
        float x = event.getRawX();
        float y = event.getRawY();
        //改变椭圆形位置
        lx = x - cx;
        rx = x + cx;
        ty = y - cy;
        by = y + cy;
        //对View进行重新绘制
        postInvalidate();
        */
        postInvalidate();
        return true;
    }

    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    void drawArrow(Canvas ctx, int fromX, int fromY, int toX, int toY,int theta,int headlen,Paint paint) {

        // 计算各角度和对应的P2,P3坐标
        double angle = Math.atan2(fromY - toY, fromX - toX) * 180 / Math.PI,
                angle1 = (angle + theta) * Math.PI / 180,
                angle2 = (angle - theta) * Math.PI / 180,
                topX = headlen * Math.cos(angle1),
                topY = headlen * Math.sin(angle1),
                botX = headlen * Math.cos(angle2),
                botY = headlen * Math.sin(angle2);

        ctx.save();

        Path path = new Path();

        double arrowX = fromX - topX,
                arrowY = fromY - topY;

        path.moveTo((float) arrowX, (float)arrowY);
        path.moveTo(fromX, fromY);
        path.lineTo(toX, toY);
        arrowX = toX + topX;
        arrowY = toY + topY;
        path.moveTo((float)arrowX, (float)arrowY);
        path.lineTo(toX, toY);
        arrowX = toX + botX;
        arrowY = toY + botY;
        path.lineTo((float)arrowX, (float)arrowY);


        ctx.drawPath(path, paint);
        ctx.drawLine(fromX,fromY,toX,toY,paint);
    }

        @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public void run() {
        this.invalidate();
        // 重新设置定时器，在60秒后调用run方法
        handler.postDelayed(this, 1000);
    }

    public void externOnDraw()
    {
        postInvalidate();
    }
}
